package com.cloudbroker.bcs.common.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.codec.binary.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DigestUtil {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(DigestUtil.class);
    
    private static MessageDigest getDigest(String algorithm) {
        try {
            return MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e) {
            LOGGER.error("get digest failed", e);
        }
        return null;
    }
    
    private static MessageDigest getMD5Digest() {
        return getDigest("MD5");
    }
    
    private static MessageDigest getSHA1Digest() {
        return getDigest("SHA-1");
    }
    
    private static MessageDigest getSHA256Digest() {
        return getDigest("SHA-256");
    }
    
    private static MessageDigest getSHA512Digest() {
        return getDigest("SHA-512");
    }
    
    public static String MD5(byte[] data) {
        byte[] digestData = getMD5Digest().digest(data);
        return Hex.encodeHexString(digestData);
    }
    
    public static String SHA1(byte[] data) {
        byte[] digestData = getSHA1Digest().digest(data);
        return Hex.encodeHexString(digestData);
    }
    
    public static String SHA256(byte[] data) {
        byte[] digestData = getSHA256Digest().digest(data);
        return Hex.encodeHexString(digestData);
    }
    
    public static String SHA512(byte[] data) {
        byte[] digestData = getSHA512Digest().digest(data);
        return Hex.encodeHexString(digestData);
    }
    
    public static String digest(String algorithm, byte[] data) {
        byte[] digestData = getDigest(algorithm).digest(data);
        return Hex.encodeHexString(digestData);
    }
    
}
